package org.example;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class QuestionAnswerApp {

    private JFrame frame;
    private JTable table;
    private DefaultTableModel tableModel;

    public QuestionAnswerApp() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame("Question Answer App");
        frame.setBounds(100, 100, 600, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout(0, 0));

        // Create table model with headers
        String[] columns = {"Question", "Answer"};
        tableModel = new DefaultTableModel(columns, 0);
        table = new JTable(tableModel);

        // Add table to scroll pane
        JScrollPane scrollPane = new JScrollPane(table);
        frame.add(scrollPane, BorderLayout.CENTER);

        // Create panel for buttons
        JPanel panel = new JPanel();
        JButton btnAddRow = new JButton("Add Row");
        JButton btnGenerateExcel = new JButton("Generate Excel");
        panel.add(btnAddRow);
        panel.add(btnGenerateExcel);
        frame.add(panel, BorderLayout.SOUTH);

        // Add row action
        btnAddRow.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addRow();
            }
        });

        // Generate Excel action
        btnGenerateExcel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                generateExcel();
            }
        });

        frame.setVisible(true);
    }

    private void addRow() {
        // Create dialog for user input
        JDialog dialog = new JDialog(frame, "Add Question and Answer", true);
        dialog.setSize(300, 150);
        dialog.setLayout(new GridLayout(3, 2));

        JLabel lblQuestion = new JLabel("Question:");
        JLabel lblAnswer = new JLabel("Answer:");
        JTextField txtQuestion = new JTextField(20);
        JTextField txtAnswer = new JTextField(20);

        dialog.add(lblQuestion);
        dialog.add(txtQuestion);
        dialog.add(lblAnswer);
        dialog.add(txtAnswer);

        JButton btnOK = new JButton("OK");
        dialog.add(btnOK);

        btnOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String question = txtQuestion.getText();
                String answer = txtAnswer.getText();
                if (!question.isEmpty() && !answer.isEmpty()) {
                    tableModel.addRow(new Object[]{question, answer});
                }
                dialog.dispose();
            }
        });

        dialog.setVisible(true);
    }

    private void generateExcel() {
        List<String[]> data = new ArrayList<>();
        int rowCount = tableModel.getRowCount();
        for (int i = 0; i < rowCount; i++) {
            data.add(new String[]{
                    (String) tableModel.getValueAt(i, 0),
                    (String) tableModel.getValueAt(i, 1)
            });
        }

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");

        // Create header row
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("Question");
        headerRow.createCell(1).setCellValue("Answer");

        // Create data rows
        int rowNum = 1;
        for (String[] rowData : data) {
            Row row = sheet.createRow(rowNum++);
            row.createCell(0).setCellValue(rowData[0]);
            row.createCell(1).setCellValue(rowData[1]);
        }

        try (FileOutputStream fileOut = new FileOutputStream("questions_answers.xlsx")) {
            workbook.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(frame, "Error generating Excel file", "Error", JOptionPane.ERROR_MESSAGE);
        } finally {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        JOptionPane.showMessageDialog(frame, "Excel file generated successfully", "Success", JOptionPane.INFORMATION_MESSAGE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new QuestionAnswerApp();
            }
        });
    }
}
